package com.ermao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ermao.mapper.AccountMapper;
import com.ermao.mapper.UserMapper;
import com.ermao.pojo.Account;
import com.ermao.pojo.User;
import com.ermao.pojo.result.analysis.BillMonth;
import com.ermao.pojo.result.analysis.BillYear;
import com.ermao.service.AnalysisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Ermao
 * Date: 2021/9/8 13:53
 */
@Log4j2
@Service
public class AnalysisServiceImpl implements AnalysisService {

	private AccountMapper accountMapper;
	private UserMapper userMapper;

	@Autowired
	public AnalysisServiceImpl(AccountMapper accountMapper, UserMapper userMapper) {
		this.accountMapper = accountMapper;
		this.userMapper = userMapper;
	}

	@Override
	public List<Account> selectDetails(Integer userId, Date fromTime, Date endTime) {
		User user = userMapper.selectById(userId);
		if (user == null) {
			return null;
		}
		QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("account_id", "account_amount", "account_type", "category_id", "account_time", "account_comment");
		queryWrapper.ge("account_time", fromTime).le("account_time", endTime);
		queryWrapper.eq("user_id", userId);
		queryWrapper.orderByDesc("account_time");
		return accountMapper.selectList(queryWrapper);
	}

	@Override
	public BillYear selectBill(Integer userId, Date fromTime, Date endTime) {

		List<BillMonth> list = new ArrayList<>();
		// 拿到年和月
		int year = fromTime.getYear() + 1900;
		int month = endTime.getMonth() + 1;
		// 组装日期
		LocalDate date = LocalDate.of(year, 1, 1);
		for (int i = 1; i <= month; i++) {
			LocalDate endOfMonth = date.plusMonths(1).minusDays(1);
			QueryWrapper<Account> queryWrapperOut = new QueryWrapper<>();
			queryWrapperOut.select("sum(account_amount)").eq("account_type", 1).eq("user_id", userId);
			queryWrapperOut.ge("account_time", date).le("account_time", endOfMonth);
			List<Object> objectsOut = accountMapper.selectObjs(queryWrapperOut);
			Double monthOutAccountAmount = (Double) objectsOut.get(0);

			QueryWrapper<Account> queryWrapperIn = new QueryWrapper<>();
			queryWrapperIn.select("sum(account_amount)").eq("account_type", 0).eq("user_id", userId);
			queryWrapperIn.ge("account_time", date).le("account_time", endOfMonth);
			List<Object> objectsIn = accountMapper.selectObjs(queryWrapperIn);
			Double monthInAccountAmount  = (Double) objectsIn.get(0);

			BillMonth billMonth = new BillMonth(0, 0d, 0d);
			billMonth.setMonth(i);
			if (monthOutAccountAmount != null) {
				billMonth.setMonthOutAccountAmount(monthOutAccountAmount);
			}
			if (monthInAccountAmount != null) {
				billMonth.setMonthInAccountAmount(monthInAccountAmount);
			}
			list.add(billMonth);
			date = date.plusMonths(1);
		}
		BillYear bill = new BillYear(0d, 0d, 0d, null);
		list.forEach(item -> {
			bill.setTotalExpend(bill.getTotalExpend() + item.getMonthOutAccountAmount());
			bill.setTotalIncome(bill.getTotalIncome() + item.getMonthInAccountAmount());
		});
		bill.setTotalAmount(bill.getTotalIncome() - bill.getTotalExpend());
		bill.setList(list);
		return bill;
	}
}
